<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\common\model\Area;
use app\common\facade\Builder;
use think\exception\ValidateException;

class OrderRefund extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\OrderRefund';

    //关联模型
    protected array $hasOne = ['user','order'];

    protected int $takeOver = 3;

    public function index(): string
    {
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'title' => '订单ID', 'width' => 90])
            ->addColumn(['field' => 'product', 'title' => '订单商品'])
            ->addColumn(['field' => 'sn', 'title' => '订单编号', 'templet' => '#orderrefundTpl', 'width' => 250])
            ->addColumn(['field' => 'money', 'title' => '退款金额'])
            ->addColumn(['field' => 'code', 'title' => '下单会员', 'templet' => '#_userTpl', 'width' => 250])
            ->addColumn(['field' => 'create_time', 'title' => '申请退款时间'])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#orderDemo', 'title' => '操作', 'width' => 250])
            ->addTopButtons(['deleteAll', 'export'])
            ->addRightButtons([
                'deliver'    => ['title' => '退款详情', 'size' => '1200,1080', 'url' => url('orderrefund/deliver')->build(), 'event' => '_openNewPage'],
                'delete'
            ])
            ->addSearch([
                ['uid', '会员编号/昵称/手机号', 'input', '', '\app\common\model\User', 'code|nickname|mobile,id'],
                ['sn', '订单编号', 'input', '', '\app\common\model\Order', 'sn,id'],
                ['status', '退款状态', 'select', getOrderRefundStatus('', 2)]
            ])
            ->addJsUrl(['order'])
            ->addExtraHtml(['orderrefund'])
            ->fetch();
    }

    public function edit($id = '', $tpl = 'orderrefund/edit', $info = '')
    {
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }

    public function deliver($id = '')
    {

        $info = $this->model::find($id);
        $info->imageslist=explode(',',$info->images);
        View::assign([
            'express' => \app\common\model\Express::where(['status' => 1, 'is_del' => 0])->select(),
            'info'    => $info
        ]);
        return View::fetch('orderrefund/deliver');
    }
    public function refund(){

        $param = $this->request->param();
        try {
            $this->validate($param, 'Refund');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        return json($this->model::deliver($param, $this->admin));
    }
    public function getArea($id, $level = 0)
    {
        return Area::getArea($id ?: 100000, $level);
    }

}
